package com.wjb.xietong.server.topic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.util.ContentReplace;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class GetCommentListResponse extends BaseResponseDataParse {

	private static GetCommentListResponse instance = null;
	private HeaderInfoResponse headerInfo = null;
	private List<CommentInfo> commentInfoList = null;

	private GetCommentListResponse() {

	}

	public static synchronized GetCommentListResponse instance() {
		if (null == instance) {
			instance = new GetCommentListResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setCommentInfoList(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.REPLY_TOPIC_MAP_LIST)) {
			JSONArray jsonArray = response
					.optJSONArray(IDs.REPLY_TOPIC_MAP_LIST);
			if (null != jsonArray) {
				commentInfoList = new ArrayList<GetCommentListResponse.CommentInfo>();
				for (int i = 0; i < jsonArray.length(); i++) {
					CommentInfo commentInfo = new CommentInfo();
					JSONObject jsonObj = jsonArray.optJSONObject(i);

					if (jsonObj.has(IDs.ID)) {
						commentInfo.setId(jsonObj.optLong(IDs.ID));
					}
					if (jsonObj.has(IDs.TOPIC_ID)) {
						commentInfo.setTopic_id(jsonObj.optLong(IDs.TOPIC_ID));
					}
					if (jsonObj.has(IDs.TOPIC_TITLE)) {
						commentInfo.setTopic_title(jsonObj
								.optString(IDs.TOPIC_TITLE));
					}
					if (jsonObj.has(IDs.USER_ID)) {
						commentInfo.setUser_id(jsonObj.optLong(IDs.USER_ID));
					}
					if (jsonObj.has(IDs.TOPIC_OWNER)) {
						commentInfo.setTopic_owner(jsonObj
								.optLong(IDs.TOPIC_OWNER));
					}
					if (jsonObj.has(IDs.CONTENT)) {
						commentInfo.setContent(jsonObj.optString(IDs.CONTENT));
					}
					if (jsonObj.has(IDs.GMT_CREATE)) {
						commentInfo.setGmt_create(jsonObj
								.optString(IDs.GMT_CREATE));
					}
					if (jsonObj.has(IDs.CREATE_BY_USER_ID)) {
						commentInfo.setCreate_by_user_id(jsonObj
								.optLong(IDs.CREATE_BY_USER_ID));
					}
					if (jsonObj.has(IDs.GMT_MODIFIED)) {
						commentInfo.setGmt_modified(jsonObj
								.optString(IDs.GMT_MODIFIED));
					}
					if (jsonObj.has(IDs.MODIFIED_BY_USER_ID)) {
						commentInfo.setModified_by_user_id(jsonObj
								.optLong(IDs.MODIFIED_BY_USER_ID));
					}
					if (jsonObj.has(IDs.USER_NICK2)) {
						commentInfo.setUser_nick(jsonObj
								.optString(IDs.USER_NICK2));
					}
					commentInfoList.add(commentInfo);
				}
				setCommentInfoList(commentInfoList);
			}
			return true;
		}
		return false;
	}

	public static class CommentInfo {
		private long id = 0l;
		private long topic_id = 0l;
		private String topic_title = null;
		private long user_id = 0l;
		private long topic_owner = 0l;
		private String content = null;
		private String gmt_create = null;// 2014-11-14 10:08:59
		private long create_by_user_id = 0l;
		private String gmt_modified = null;// 2014-11-14 10:08:59
		private long modified_by_user_id = 0l;
		private String user_nick = null;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public long getTopic_id() {
			return topic_id;
		}

		public void setTopic_id(long topic_id) {
			this.topic_id = topic_id;
		}

		public String getTopic_title() {
			return topic_title;
		}

		public void setTopic_title(String topic_title) {
			this.topic_title = topic_title;
		}

		public long getUser_id() {
			return user_id;
		}

		public void setUser_id(long user_id) {
			this.user_id = user_id;
		}

		public long getTopic_owner() {
			return topic_owner;
		}

		public void setTopic_owner(long topic_owner) {
			this.topic_owner = topic_owner;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = ContentReplace.replaceContent(content);
		}

		public String getGmt_create() {
			return gmt_create;
		}

		public void setGmt_create(String gmt_create) {
			this.gmt_create = gmt_create;
		}

		public long getCreate_by_user_id() {
			return create_by_user_id;
		}

		public void setCreate_by_user_id(long create_by_user_id) {
			this.create_by_user_id = create_by_user_id;
		}

		public String getGmt_modified() {
			return gmt_modified;
		}

		public void setGmt_modified(String gmt_modified) {
			this.gmt_modified = gmt_modified;
		}

		public long getModified_by_user_id() {
			return modified_by_user_id;
		}

		public void setModified_by_user_id(long modified_by_user_id) {
			this.modified_by_user_id = modified_by_user_id;
		}

		public String getUser_nick() {
			return user_nick;
		}

		public void setUser_nick(String user_nick) {
			this.user_nick = user_nick;
		}

	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<CommentInfo> getCommentInfoList() {
		return commentInfoList;
	}

	public void setCommentInfoList(List<CommentInfo> commentInfoList) {
		this.commentInfoList = commentInfoList;
	}

}
